package mysql

import (
	"fmt"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
)

var DB *gorm.DB
var Dbprefix string = "dcim_"

func init() {

	dsn := "idc_data:#sfsuK3q@tcp(172.24.131.71:23306)/tnebula?charset=utf8mb4&parseTime=True&loc=Local"
	// mysqldb, errm := sql.Open("mysql", dsn)
	// if errm != nil {
	// 	fmt.Printf("mysql connect fail")
	// }

	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			TablePrefix:   Dbprefix, //表前缀
			SingularTable: true,
		},
		Logger:                 logger.Default.LogMode(logger.Silent),
		PrepareStmt:            false, //生成sql缓存-提高性能
		SkipDefaultTransaction: false, //默认gin开启了增删改的事务-此选项可以跳过事务增加性能
	})
	if err != nil {
		fmt.Println(err.Error())
		fmt.Print("数据库连接失败")
	}
	DB = db
	// fmt.Println("数据连接成功")
}

func Dbfix() string {
	return Dbprefix
}
